package joindemo.service.impl;

import joindemo.domain.po.ArticleStatPo;
import joindemo.domain.vo.ArticleStatVo;
import joindemo.mapper.ArticleStatMapper;
import joindemo.service.ArticleStatService;
import joindemo.utils.RedisUtil;
import joindemo.common.constants.CommonRedisKeyConstant;
import lombok.RequiredArgsConstructor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
@MapperScan("joindemo.mapper")
public class ArticleStatServiceImpl implements ArticleStatService, CommonRedisKeyConstant {
    private final ArticleStatMapper articleStatMapper;
    private final RedisUtil redisUtil;

    private String buildKey(Long articleId) {
        return ARTICLE_STAT_KEY_PREFIX + articleId;
    }

    @Override
    public void initArticleStat(Long articleId){
        ArticleStatPo po = articleStatMapper.selectById(articleId);
        if (po != null) {
            return;
        }
        po = ArticleStatPo.builder()
                .articleId(articleId)
                .viewCount(0L)
                .likeCount(0L)
                .commentCount(0L)
                .favoriteCount(0L)
                .build();
        articleStatMapper.insert(po);

        String key = buildKey(articleId);
        Map<String, Object> map = new HashMap<>();
        map.put("viewCount", 0L);
        map.put("likeCount", 0L);
        map.put("commentCount", 0L);
        map.put("favoriteCount", 0L);
        redisUtil.hmset(key, map);
    }

    @Override
    public void addViewCount(Long articleId){}

    @Override
    public void addLikeCount(Long articleId){}

    @Override
    public void decreaseLikeCount(Long articleId){}

    @Override
    public void addCommentCount(Long articleId){}

    @Override
    public void decreaseCommentCount(Long articleId){}

    @Override
    public void addFavoriteCount(Long articleId){}

    @Override
    public void decreaseFavoriteCount(Long articleId){}

    @Override
    public ArticleStatVo getArticleStat(Long articleId){
        return null;
    }
}
